<template>
	<view>
		<view class="page_header">
			<navigation>个人中心</navigation>
			<view class="info_panel">
				<view class="info_left">
					<view class="top">
						<view  v-if="userInfo.wxNickname" ref="log"  class="name">{{userInfo.wxNickname}}</view>
						<button class="login" v-else  @click="getUserProfile"> 登录 </button>
						<view class="hint"></view>
					</view>
<!-- 					<view class="top">
						<button class="login" @click="gore">登录</button>
					</view> -->
					<!-- <view class="bottom">高龄津贴补助需再次认证</view> -->
				</view>
				<view class="info_right">
					<image class="user_photo" :src="userInfo.wxAvatar" mode=""></image>
				</view>
			</view>
		</view>
		<view class="page_panel"></view>
		<scroll-view scroll-y="true" class="page_scroll" scroll-with-animation="true" enable-back-to-top="true">
			<view class="module">
				<view class="grid">
					<view class="grid_item" v-for="(item,index) in moduleList" :key="index" @tap="goOrder()">
						<image class="grid_image" :src="item.image" mode="aspectFit"></image>
						<view class="grid_text"><text>{{item.text}}</text></view>
					</view>
				</view>
			</view>
			<view class="domain">
				<view class="domain_panel">
					<view class="title">常用功能</view>
					<view class="grid">
						<view class="grid_item" v-for="(item,index) in domainList" :key="index" @tap="goPage(item)">
							<image class="grid_image" :src="item.image" mode="aspectFit"></image>
							<view class="grid_text"><text>{{item.text}}</text></view>
						</view>
					</view>
				</view>
			</view>
			<view class="advertising_panel">
				<image class="advertising_image" src="https://images-vx.oss-cn-beijing.aliyuncs.com/images-vx/advertising01.png" mode=""></image>
			</view>
		</scroll-view>
		<view class="getPhone" v-if="gettel==1" >
			<view class="esc" @click="close">
				X
			</view>
			<view class="getPhoneChild">
				<image src="../../static/images/logo.png" mode=""></image>
				<view class="btn">
					
					<button   open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">点击获取手机号</button>
				</view>
			</view>
			
		</view>
	</view>
</template>

<script>
	import {
		HTTP_SERVER_URL,
		STATUS_CODE
	} from '../../config/index.js';
	export default {
		data() {
			return {
				code:null,
				img:null,
				name:null,
				after:false,
				openId:null,
				sessionKey:null,
				encryptedData:null,
				iv:null,
				userData:null,
				gettel:0,
				userInfo:this.$store.getters.getUserInfo,
				moduleList: [{
						"image": "https://images-vx.oss-cn-beijing.aliyuncs.com/images-vx/model01.png",
						"text": "已预约",
						isTabbar: false
					},
					{
						"image": "https://images-vx.oss-cn-beijing.aliyuncs.com/images-vx/model02.png",
						"text": "已服务",
						isTabbar: false
					},
					{
						"image": "https://images-vx.oss-cn-beijing.aliyuncs.com/images-vx/model03.png",
						"text": "待评价",
						isTabbar: false
					},
					{
						"image": "https://images-vx.oss-cn-beijing.aliyuncs.com/images-vx/model04.png",
						"text": "全部订单",
						isTabbar: false
					}
				],
				domainList: [{
						"image": "https://images-vx.oss-cn-beijing.aliyuncs.com/images-vx/domain01.png",
						"text": "帮录津贴",
						url: '/pages/index/helpallowance/helpallowance',
						isTabbar: false
					},
					{
						"image": "https://images-vx.oss-cn-beijing.aliyuncs.com/images-vx/domain02.png",
						"text": "我的津贴",
						url: '/pages/index/allowance/allowance',
						isTabbar: false
					},
					{
						"image": "https://images-vx.oss-cn-beijing.aliyuncs.com/images-vx/domain03.png",
						"text": "意见反馈",
						url: '/pages/index/feedback/feedback',
						isTabbar: false
					},
					// {
					// 	"image": "https://images-vx.oss-cn-beijing.aliyuncs.com/images-vx/domain04.png",
					// 	"text": "日常问题",
					// 	url: '',
					// 	isTabbar: false
					// }
				]
			};
		},
		created() {
			this.userInfo=uni.getStorageSync('open')
			wx.login({
				provider:'weixin',
				success: (res) => {
					this.code=res.code
					uni.request({
						url:HTTP_SERVER_URL+'/aged/user/appletsLogin',
									method:"POST",
									data:{
										code:this.code
									},
									success:(res)=>{
										this.openId=res.data.data.openId
										this.sessionKey=res.data.data.sessionKey
										console.log('wx.login返回值',res)
									},
									fail:(res)=>{
										console.log('失败失败',res)
									}
					})
					
				}
			})
		},
		onShow() {
			
		},
		methods: {
			gore(){
				uni.navigateTo({
					url:'../system/rigest'
				})
			},
			close(){
				this.gettel=0
			},
			getPhoneNumber:function(e){
				//根据openid等获取用户信息
				// console.log('sssss')
				this.gettel=0
			        if(e.detail.errMsg=="getPhoneNumber:fail user deny"){
						this.img=''
						this.name=''
			        }else{       
						this.encryptedData =e.detail.encryptedData 
						this.iv=e.detail.iv
			            e.detail.encryptedData      //加密的用户信息  
			            e.detail.iv     //加密算法的初始向量  时要用到
						console.log('加密内容',this.encryptedData)
						console.log('ivvvvvvvvvvvvvvvvvvvvvvvv',this.iv)
			        
					//获取openid之后发送请求登录
					 uni.request({
					 	url:HTTP_SERVER_URL+'/aged/user/login',
					 				method:"POST",
					 				data:{
					 					openId:this.openId,
										sessionKey:this.sessionKey,
										encryptedData:this.encryptedData,
										iv:this.iv,
										wxAvatar: this.img,
										wxNickname:this.name
					 				},
					 				success:(res)=>{
										uni.setStorageSync('phone',res.data.data.phone)
										if(res.data.data.isUser==0){
											uni.showToast({
												title:'您还未注册，请先完成注册再进行以下操作',
												position:"center",
												icon:"none"
											})
											setTimeout(()=>{
												uni.navigateTo({
													url:'../system/rigest'
												})
											},2000)
											
										}else if(res.data.data.isUser==3){
											uni.showToast({
												title:'您的信息正在审核中，请稍候...',
												position:"center",
												icon:"none"
											})
										}else if(res.data.data.isUser==4){
											uni.showToast({
												title:'你的审核未通过，请完善信息后再次提交审核',
												position:"center",
												icon:"none"
											})
										}else{
											let userData=res.data.data
											userData.wxAvatar=this.img
											userData.wxNickname=this.name
											uni.clearStorageSync('open')
											uni.setStorageSync('open',userData)
											this.userInfo=uni.getStorageSync('open')
											   this.userInfo.wxNickname=this.name
											   this.userInfo.wxAvatar=this.img
											
											
											this.$store.dispatch('wechatLogin', userData).then(res => {
												setTimeout(() => {
													uni.switchTab({
														url: '/pages/index/index'
													});
												}, 1100);
											});
											
										}
										
					 				},
					 })
					 
					 } 
					 
					 
					 
					 
					
			    } ,
			getUserProfile(e) {
			    // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认
			    // 开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
			    wx.getUserProfile({
			      desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
			      success: (res) => {
					 
			    //    this.userInfo.wxNickname=res.userInfo.nickName
				   // this.userInfo.wxAvatar=res.userInfo.avatarUrl
				   this.img= res.userInfo.avatarUrl
				   this.name=res.userInfo.nickName
					console.log(res)
			      }
			    })
				this.gettel=1
			  },
			
			// Truemess(){
			// 	this.userInfo=uni.getStorageSync('open')
			// },
			gologin(){
					setTimeout(()=>{
							uni.navigateTo({
								url:'../system/login',
							})
					},500)
			},
			goOrder() {
				uni.navigateTo({
					url: '/pages/mine/order/order'
				});
			},
			goPage(item) {
				if(item.text=='帮录津贴'){
					let sys=uni.getStorageSync('open').haveSys
					if(sys!=1){
						uni.showToast({
							title:'您暂时没有该权限',
							icon:'none',
							position:'center'
						})
					}else{
						uni.navigateTo({
							url: item.url
						});
					}
					//待删除
					uni.navigateTo({
						url: item.url
					});
				}else if(item.text=="我的津贴"){
					let userInfo = uni.getStorageSync('open');
					console.log('跳转用',userInfo)
					console.log(userInfo.haveAllowance)
					// uni.navigateTo({
					// 	url:item.url
					// })
					if(userInfo.haveAllowance == 1) {
						uni.navigateTo({ 
							url: '/pages/index/allowanceAudit/allowanceAudit'
						});
					} else {
						uni.navigateTo({ 
							url: item.url
						});
					}
				} else if (item.url && item.isTabbar) {
					uni.switchTab({
						url: item.url
					});
				} else if (item.url && !item.isTabbar) {
					uni.navigateTo({
						url: item.url
					});
				} else {
					uni.showToast({
						title: '功能模块正在开发中！',
						icon: 'none',
						duration: 1000
					});
				}
			}
		}
	}
</script>

<style lang="stylus" scoped>
	.getPhoneChild{
		background-color #FFFFFF
		width 90%
		border-radius 20rpx
		padding-bottom 50rpx
		margin 700rpx auto 200rpx
		image{
			width 200rpx
			height 200rpx
			margin 90rpx auto 70rpx
		}
	}
	.btn{
		button{
			background-color #FF7D21
			width 60%
			color #FFFFFF
			border-radius 50rpx
		}
	}
	.getPhone{
		position fixed
		bottom 0
		
		height 100%
		z-index 1000
		width 100%
		background-color #fff
		text-align center
		padding 20rpx
		background rgba(0,0,0,0.5)
		.esc{
			position absolute
			top 735rpx
			right 80rpx
			width 60rpx
			height 60rpx
			text-align center
			line-height 60rpx
			background rgba(0,0,0,.2) 
			border-radius 50%
		}
		
	}
	.login{
		background rgba(0,0,0,0)
		color #FFFFFF
		font-size 36rpx
		font-weight 500
		font-family PingFang SC;
		
	}
	button::after { border: none }
	
	.page_scroll {
		position absolute;
		top calc(350rpx + var(--status-bar-height));
		right 0rpx;
		bottom 0rpx;
		left 0rpx;
		z-index 99; 
	}

	.page_panel {
		position absolute;
		top calc(435rpx + var(--status-bar-height));
		right 0rpx;
		bottom 0rpx;
		left 0rpx;
		z-index 98;
		background-color rgba(247, 247, 247, 1);
	}
	
	

	.info_panel {
		width 100%;
		height 230rpx;
		padding 0rpx 30rpx 10rpx 30rpx;
		box-sizing border-box;
		display flex;
		align-items center;
		justify-content space-between;

		.info_left {
			.top {
				display flex;

				.name {
					font-size: 36rpx;
					font-family: PingFang SC;
					font-weight: 500;
					color: #FFFFFF;
				}
			}

			.bottom {
				margin-top 20rpx;
				font-size: 24rpx;
				font-family: PingFang SC;
				font-weight: 400;
				color: rgba(255, 255, 255, 0.6);
			}
		}

		.info_right {
			.user_photo {
				width: 160rpx;
				height: 160rpx;
				background: #EAEAEA;
				border: 2rpx solid #FFFFFF;
				border-radius 50%;
			}
		}
	}

	.module,
	.domain {
		width: 100%;
		padding: 0rpx 30rpx;
		box-sizing: border-box;

		.grid {
			padding: 35rpx 0rpx;
			background-color #fff;
			display: flex;
			flex-wrap: wrap;
			justify-content: space-between;
			border-radius: 20rpx;

			.grid_item {
				flex 1;
				display: flex;
				flex-direction: column;
				align-items: center;
				justify-content: center;

				.grid_image {
					width: 60rpx;
					height: 60rpx;
				}

				.grid_text {
					width 100%;
					text-align center;
					font-size: 24rpx;
					font-family: PingFang SC;
					font-weight: 400;
					color: #333333;
					margin-top: 10rpx;
				}
			}
		}
	}

	.domain {
		margin-top 30rpx;

		.domain_panel {
			background-color #FFFFFF;
			border-radius: 20rpx;

			.title {
				padding-top 30rpx;
				padding-left 24rpx;
				font-size: 36rpx;
				font-family: PingFang SC;
				font-weight: 500;
				color: #322F2D;
				box-sizing border-box;
			}

			.grid {
				justify-content flex-start;

				.grid_item {
					flex none;
					width 25% !important;

					.grid_image {
						width 106rpx !important;
						height 106rpx !important;
					}

					.grid_text {
						font-size: 24rpx;
						font-family: PingFang SC;
						font-weight: 400;
						color: #322F2D;
						margin-right: 15rpx;
					}
				}

			}

		}
	}

	.advertising_panel {
		width 100%;
		padding 30rpx 30rpx 0rpx 30rpx;
		box-sizing border-box;

		.advertising_image {
			width 100%;
			height 206rpx;
			border-radius 6rpx;
		}

	}
</style>
